; MXC-ADS1.Z80  Revision 1.0    Jim Lill   19 Mar 87
;
; MEXplus Clock Overlay For Apple //e using Dallas DS1216E or
; other type RTC that plug into the CD ROM socket.
;
; Requires: DSCLK.DVR (See PCPITIME.LBR)
;
;------------------------------------------------------------------------
; revision |  date  |		    description 	   |	author
;----------+-------------------------------------------------------------
;   1.0    |19Mar87 | created original, some code from     | Jim Lill
;          |        | MXC-DS10.Z80, DSDSCLK.Z80            |
;------------------------------------------------------------------------
bdos	equ	0005h
cent	equ	1900		;century
dvr3	equ	0ffe3h		;these two locations are in the 
dvr0	equ	0ffe0h		;6502 I/O area for talking to .DVR
;...............................
;
; Prtval macro prints text and value during assembly
;
prtval	macro msg,n
	.printx	msg n
	endm
;...............................
;
; start of MEX stuff....

	org	0100h
	db	0C3h		; this is for 8080 processor
;...............................
;
; JMP table for clock overlay
;
	org	0E00h		;start of clock overlay

start:	jp	gettim		;get time
	jp	getdat		;get date
	ret			;cset processor
	nop
	nop
	ret			;clock overlay init
	nop
	nop
	ret			;clock overlay de-init
	nop
	nop
;...............................
;
; gettim, this routine gets the time in clktbl and readies it for MEX
;
;	B - hours (0-23)
;	C - minutes (0-59)
;	D - seconds (0-59)
;	E - hundredths of sec (0-99)
;
gettim:
	call	clock 		; get date and put BCD data in CLKTBL
	
	ld 	A,(hour)	; load packed BCD hours into A
	call	bcd2bin		; \__ place bin hours in B
	ld	B,A		; /
	
	ld	A,(min)		; load packed BCD mins into A
	call	bcd2bin		; \__ place bin mins in C
	ld	C,A		; /

	ld	A,(sec)		; load packed BCD secs into A
	call	bcd2bin		; \__ place bin secs in D
	ld	D,A		; /

	ld	A,(hsec)	; load packed BCD hsecs into A
	call	bcd2bin		; \_ place bin hsecs in E
	ld	E,A		; /

	ret			; back to MEX
;................................
;
; getdat, this routine gets the date in clktbl and readies it for MEX
;
;	BC - year (1985-2099)
;	D - month (1=jan, 2=feb, etc)
;	E - day of month (1-31)
;
getdat:
	call	clock 		; get date and put BCD data in CLKTBL
				;
	ld 	A,(day)		; load packed BCD day into A
	call	bcd2bin		; \__ place bin day in E
	ld	E,A		; /

	ld	A,(mon)		; load packed BCD mon into A
	call	bcd2bin		; \__ place bin mon in D
	ld	D,A		; /

	ld	A,(year)	; load packed BCD year (00-99) into A
	call	bcd2bin		; \__ place bin year in C
	ld	C,A		; /
	ld	B,0		; make a whole year value
	ld	HL,cent		; by adding cent (eg 1900)
	add	HL,BC		; to partial year
	ld	C,L		; move answer into BC
	ld	B,H		; "     "      "  "

	ret			; back to MEX
;...............................
;
; bcd2bin --> packed BCD (A) converted to Binary (A)
;
bcd2bin:
	push	BC		; be safe
	ld	C,A		; load in value from A
	and	0F0h		; mask off lower nibble
	rra			; \
	rra   			;  \_/ move upper nibble
	rra  			;  / \ into lower nibble 
	rra			; /
	ld	B,A		; store value *1 
	add	A		; \__ *4 
	add	A		; /  
	add	B		; now *5 
	add	A		; finally *10
	ld	B,A		; store 10's digit in B
	ld	A,C		; \__ load lower digit into A	
	and	00Fh		; /
	add	B		; combine both digs. into single byte
	pop	BC		; restore register
	ret			; 
;...............................
;
; CLOCK calls the DS1216E via the PCPI driver and puts the data into 
;       CLKTBL as BCD.    

clock:	ld	c,251		;put command in C for call
	call	dvr3  		;send the command
	ld	c,16		;get time command in C
	call	dvr3		;send it
	ld	hl,clktbl	;point at table
getit:	call	dvr0		;receive a character
	cp	0AAh		;Check to see if last character
	jr	z,exit		;if it is exit loop
	ld	(hl),a		;store data in buffer
	inc	hl		;bump buffer pointer
	jr	getit		;go get new data 
exit:	ret

clktbl: 
year:	ds	1		
mon:	ds	1		
day:	ds	1		
dow:	ds	1		
hour:	ds	1		
min:	ds	1		
sec:	ds	1		
hsec:	ds	1		
;..................
;
; Assembly-time only, MEX modules have space restrictions
;
	if1
	  .radix 16
	  prtval <ending address = >,%($)
	  prtval <  overlay size = >,%($ - start)
	endif
;................................

	end			
